package com.zoweunion.mechanic.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.model.app.ViewBoardMenu;
import com.zoweunion.mechanic.service.app.ToolViewBoardService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * app端，工具看板 控制器
 *
 * @author zhangjun
 * @date 2019-08-14
 */
@RestController
@RequestMapping("/tool/viewBoard")
public class ToolViewBoardController extends BaseController {

    @Autowired
    ToolViewBoardService toolViewBoardService;


    /**
     * 主页 今日统计（今日预约，今日完成，今日受理）
     */
    @PostMapping("todayStatistics")
    public ResponseBean todayStatistics(HttpServletRequest request) throws MyException {
        logBefore(logger, "主页 今日统计（今日预约，今日完成，今日受理）");
        User currentUser = getCurrentUser(request);
        Map<String, Object> viewBoardMenuList = toolViewBoardService.todayStatistics(currentUser);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", viewBoardMenuList);
    }

    /**
     * 获取工单列表
     */
    @PostMapping("listOrderInfo")
    public ResponseBean listOrderInfo(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取工单列表");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderList = toolViewBoardService.listOrderInfo(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderList);
    }

    /**
     * 主页 实时工单（临时展示和更多展示）
     */
    @PostMapping("listRealTimeOrder")
    public ResponseBean listRealTimeOrder(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "主页 实时工单（临时展示和更多展示）");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> viewBoardMenuList = toolViewBoardService.listRealTimeOrder(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", viewBoardMenuList);
    }

    /**
     * 首页常用应用 获取看板内的应用菜单列表
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listMenu")
    public ResponseBean listMenu(HttpServletRequest request) throws MyException {
        logBefore(logger, "获取看板内的应用菜单列表");
        User currentUser = getCurrentUser(request);
        List<ViewBoardMenu> viewBoardMenuList = toolViewBoardService.listMenu(currentUser);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", viewBoardMenuList);
    }

    /**
     * 故障类型统计 饼图
     * 车主和服务商页面之间的区别，就在于是否传递oId,传参就是车主页面
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("pieFaultTypeStatistics")
    public ResponseBean pieFaultTypeStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "故障类型统计 饼图");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> faultTypeStatisticsList = toolViewBoardService.pieFaultTypeStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", faultTypeStatisticsList);
    }

    /**
     * 工单类型统计 饼图
     * 车主和服务商页面之间的区别，就在于是否传递oId,传参就是车主页面
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("pieOrderTypeStatistics")
    public ResponseBean pieOrderTypeStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工单类型统计 饼图");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderTypeStatisticsList = toolViewBoardService.pieOrderTypeStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderTypeStatisticsList);
    }

    /**
     * 工单统计 (预约工单，完成工单，总工单数)
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getOrderStatisticsData")
    public ResponseBean orderStatisticsData(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工单统计");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsData = toolViewBoardService.getOrderStatisticsData(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsData);
    }

    /**
     * 工单统计-柱状图 (预约单数，完成单数)
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("orderStatisticsHistogram")
    public ResponseBean orderStatisticsHistogram(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工单统计-柱状图 (预约单数，完成单数)");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsDataList = toolViewBoardService.orderStatisticsHistogram(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 维修统计-工单统计(完成工单，返修率，均维修时间)
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getRepairOrderStatistics")
    public ResponseBean getRepairOrderStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "维修统计-工单统计(完成工单，返修率，均维修时间)");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsData = toolViewBoardService.getRepairOrderStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsData);
    }

    /**
     * 维修统计-修工综合能力排名
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listMaintainRepairAbility")
    public ResponseBean listMaintainRepairAbility(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "维修统计-修工综合能力排名");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsDataList = toolViewBoardService.listMaintainRepairAbility(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 额度预警-信用额度预警
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listOrderWarningCreditAmount")
    public ResponseBean listOrderWarningCreditAmount(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "额度预警-信用额度预警");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.listOrderWarningCreditAmount(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 营收统计-营收统计
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getPaymentStatistics")
    public ResponseBean getPaymentStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "营收统计-营收统计");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsData = toolViewBoardService.getPaymentStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsData);
    }

    /**
     * 营收统计-营收情况
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getRevenueStatistics")
    public ResponseBean getRevenueStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "营收统计-营收情况");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.getRevenueStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 营收统计-支付类型统计
     * 车主和服务商页面之间的区别，就在于是否传递oId,传参就是车主页面
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getPaymentTypeStatistics")
    public ResponseBean getPaymentTypeStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "支付统计-支付类型统计");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.getPaymentTypeStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 客户分布-客户统计
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getCustomerStatistics")
    public ResponseBean getCustomerStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户分布-客户统计");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsData = toolViewBoardService.getCustomerStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsData);
    }

    /**
     * 客户统计-客户分布柱状图
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getCustomerDistributedStatistics")
    public ResponseBean getCustomerDistributedStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户统计-客户分布柱状图");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsData = toolViewBoardService.getCustomerDistributedStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsData);
    }

    /**
     * 客户分布-客户总数列表
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listCustomerTotalOrder")
    public ResponseBean listCustomerTotalOrder(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户分布-客户总数列表");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.listCustomerTotalOrder(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 客户分布-需维护客户列表
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listNeedMaintainCustomer")
    public ResponseBean listNeedMaintainCustomer(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户分布-需维护客户列表");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.listNeedMaintainCustomer(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * 客户信息-获取用户信息
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getUserInfoByUserId")
    public ResponseBean getUserInfoByUserId(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户信息-获取用户信息");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> orderStatisticsData = toolViewBoardService.getUserInfoByUserId(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsData);
    }

    /**
     * 客户信息-根据userid获取车辆列表
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listCarByUserId")
    public ResponseBean listCarByUserId(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户信息-根据userId获取车辆列表");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.listCarByUserId(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }


    /**
     * 客户信息-根据userid获取车辆列表
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listCarByUserIdNew")
    public ResponseBean listCarByUserIdNew(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "客户信息-根据userId获取车辆列表");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        List<Map<String, Object>> orderStatisticsDataList = toolViewBoardService.listCarByUserIdNew(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderStatisticsDataList);
    }

    /**
     * app端看板获取管辖区域
     *
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getPrecinctRegionForApp")
    public ResponseBean getPrecinctRegionForApp(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "app端看板获取管辖区域");
        // 获取请求参数
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String, Object> map = toolViewBoardService.getPrecinctRegionForApp(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", map);
    }





}
